/*
 *
 *
Copyright (c) 1998-2001 Aristar, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:

"This product includes software developed by Aristar, Inc. (http://www.aristar.com/)."

Alternately, this acknowledgment may appear in the software itself, if and
wherever such third-party acknowledgments normally appear.

4. The names "Dialect" and "Aristar, Inc." must not be used to endorse or
promote products derived from this software without prior written permission.
For written permission, please contact info@aristar.com.

5. Products derived from this software may not be called "Dialect", nor may
"Dialect" appear in their name, without prior written permission of Aristar, Inc.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
ARISTAR, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software consists of voluntary contributions made by many individuals
on behalf of Aristar, Inc.  For more information on Aristar, Inc.,
please see <http://www.aristar.com/>.

Portions of this software are based upon software freely available and
written by individuals not associated with Aristar, Inc.  Copyrights are
maintained in the relevant source code files.
 *
 *
 */

#ifndef _COUTPUT_DEVICE_H
#define _COUTPUT_DEVICE_H

#include "DConfigGlobals.h"
#include "TTable.h"

#define MAX_PAGE_HEADERS	12
#define MAX_SECTION_HEADERS	10

//====================================
#define FN_NORMAL		0
#define FN_BOLD			1
#define FN_ITALIC		2
#define FN_UNDERLINE	4

struct TSectHeader {
	TSectHeader():bPrinted(false), style(FN_BOLD){};
	TString label;
	TString data;
	U32 style;
	bool bPrinted;
};
//====================================

class COutputDev {
public:
	COutputDev();
	virtual ~COutputDev();

	virtual void StartDocument(LPCTSTR docName) = 0;
	virtual void EndDocument() = 0;
	virtual void AbortDocument();
	virtual void NewPage();
	virtual void TextOut(LPCTSTR text) = 0;
	virtual void TextOutCR(int lineHeight = -1) = 0;
	virtual void TextOutTab();
	virtual void FillTo(int column, TCHAR fillChar);
	virtual int LinesLeft() = 0;
	virtual void SetFont(TString fontName, int fontSize, U32 fontStyle) = 0;
	virtual void SetWrapIndent(int wrapIndent){m_WrapIndent = wrapIndent;};
	virtual void SetMargins(float left, float top, float right, float bottom);
	virtual void SetCursorColumn(int column){m_CurrentX = column;};
	virtual void PrintTable(const TTable& table) = 0;
	virtual void DrawLine(float x1, float y1, float x2, float y2, float width);

	void ClearHeaders();
	void SetPageHeader(int index, LPCTSTR headerText);
	void PrintPageHeaders();

	virtual void EndSection();
	virtual void EndAllSections();
	virtual void StartSection(TString& label, TString& data, U32 style);
	virtual void PrintSectionHeaders();
	virtual void SectionItem(TString& label, TString& data, TCHAR fillChar);

	virtual float GetX();
	virtual float GetY();
	virtual void SetX(float x);
	virtual void SetY(float y);
	virtual float TextWidth(TString text);

protected:
	int NumUnprintedSectHeaders();

	TString m_DocName;
	int m_PageNumber;
	bool m_bDoingSItem;
	bool m_bPrintedHeaders;
	bool m_bPrintingHeadersNow;
	bool m_bPrintingSectionHeaders;
	int m_WrapIndent;
	int m_HeaderCount;
	TString m_PageHeaders[MAX_PAGE_HEADERS];
	int m_CurrentX;
	int m_CurrentY;
	int m_TabSize;
	int m_FillToSpot;
	int m_SectHeaderTop;
	TSectHeader m_SectionHeaders[MAX_SECTION_HEADERS];
	RECT m_rcMargins;		// The actual page margins
	RECT m_rcTextMargins;	// Current margins for printing (should be subset of m_rcMargins)
	TString m_FontName;
	int m_FontSize;
	U32 m_FontStyle;
};

//==================================================================

class CEditOutputDev : public COutputDev {
public:
	CEditOutputDev(CInterpreter* pInterp);
	~CEditOutputDev();
	void ShowWindow();

	void StartDocument(LPCTSTR docName);
	void EndDocument();
	void TextOut(LPCTSTR text);
	void TextOutCR(int lineHeight = -1);
	void PrintSectionHeaders();
	void FillTo(int column, TCHAR fillChar);
	void DetermineColWidths(const TTable& table, int colWidths[]);
	void PrintTable(const TTable& table);
	int LinesLeft();
	void SetFont(TString fontName, int fontSize, U32 fontStyle);
	void SetCursorColumn(int column);
	static BOOL CALLBACK ScreenDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
protected:
	HWND m_hOutputDlg;
	CInterpreter* m_pInterp;
	static VALUE m_vHwndToDev;
};

//=============================================================

#if !defined(WIN32_PLATFORM_PSPC)

class CPrinterOutputDev : public COutputDev {
public:
	CPrinterOutputDev(LPCTSTR UNCPrinterName = NULL);
	~CPrinterOutputDev();

	void StartDocument(LPCTSTR docName);
	void EndDocument();
	void AbortDocument();
	void TextOut(LPCTSTR text);
	void _TextOut(LPCTSTR text);
	void TextOutTab();
	void TextOutCR(int lineHeight = -1);
	int LinesLeft();
	void SetFont(TString fontName, int fontSize, U32 fontStyle);
	void SetMargins(float left, float top, float right, float bottom);
	void DrawLine(float x1, float y1, float x2, float y2, float width);
	void PrintTable(const TTable& table);

	void NewPage();
	void PrintHeadersIfNeeded();
	void FillTo(int column, TCHAR fillChar);
	bool bPrintingDoc;
	bool bPrintingPage;
	TEXTMETRIC m_TextMetrics;
	int m_LineHeight;
	int m_LeftMargin;
	POINT m_ptGutter;
	POINT m_ptPageSize;
	POINT m_ptPixPerInch;

	float GetX();
	float GetY();
	void SetX(float x);
	void SetY(float y);
	float TextWidth(TString text);

protected:
	void DetermineColWidths(const TTable& table, int colWidths[], int rowHeights[]);
	void DetermineRowHeights(const TTable& table, int colWidths[], int rowHeights[]);
	int HowManyLines(LPCTSTR text, int width);
	void _HowManyLinesTextOut(LPCTSTR text, int width, int& currentX, int& lineCount);

	HDC m_hPrinterDC;
	HFONT m_hFont;
};

#endif // not defined(UNDER_CE) && !defined(WIN32_PLATFORM_PSPC)

//=============================================================================

class CRTFOutputDev : public COutputDev {
public:
	CRTFOutputDev();
	~CRTFOutputDev();

	void StartDocument(LPCTSTR docName);
	void EndDocument();
	void AbortDocument();
	void TextOut(LPCTSTR text);
	void TextOutTab();
	void TextOutCR(int lineHeight = -1);
	int LinesLeft();
	void SetFont(TString fontName, int fontSize, U32 fontStyle);
	void PrintTable(const TTable& table);
	void PrintSectionHeaders();
	void StartSection(TString& label, TString& data, U32 style);
	void EndSection();
	void SectionItem(TString& label, TString& data, TCHAR fillChar);
	void FillTo(int column, TCHAR fillChar);
protected:
	void SendLineIndent(int indent);
	TFile m_OutputFile;
	bool m_bDoc;
	int m_IndentLevel;
	bool m_bContSection;
	bool m_bDoingSItem;
	bool m_bAtLeftMargin;
};

//=============================================================================
#endif	// _COUTPUT_DEVICE_H
